﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class cardtest : MonoBehaviour
{
    [SerializeField]
    InputField input;

    public GameObject cardperfab;
    public Transform fromcard;
    public Transform tocard;
    private GameObject control;
    //private List<GameObject> cards = new List<GameObject>();
    private carddatabase card;
    public List<Card> searchedname = new List<Card>();


    // Update is called once per frame
    //void Update () {

    //       if (Input.GetKeyDown(KeyCode.Space))
    //       {
    //           addcard();
    //       }

    //}

    private void Awake()
    {
        control = GameObject.Find("cardslot");
        card = GameObject.Find("cardmanager").GetComponent<carddatabase>();
    }


    void searchcardbyname1(string name)
    {
        for (int i = 0; i < card.database.Count; i++)
        {
            if (card.database[i].Name.Contains(name))
            {
                searchedname.Add(card.database[i]);
            }
        }

    }

    void destoryslots()
    {
        if (control.transform.childCount >= 0) {
            for (int i = 0; i < control.transform.childCount; i++)
            {
                GameObject temp = control.transform.GetChild(i).gameObject;
                Destroy(temp);
            }
        }
        
    }

    public void click()
    {
        destoryslots();
        searchcardbyname1(input.text);
        addcard();
        searchedname = new List<Card>();
    }

    public void addcard()
    {
        for (int i = 0; i < searchedname.Count; i++)
        {
            GameObject go = Instantiate(cardperfab);
            go.transform.SetParent(control.transform);
            go.transform.position = fromcard.position;
            go.GetComponent<Image>().sprite = searchedname[i].Sprite;

            Vector2 toposition = new Vector2(tocard.position.x, tocard.position.y + 200f*i);
            iTween.MoveTo(go, toposition, 1f);
        }
        //cards.Add(go);
        //iTween.MoveTo(go, endcard.position, 1f);
    }
}
